package com.demostic.demostic_test.logUtils;

import android.content.Context;
import android.os.Environment;
import android.os.Process;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogcatHelper {
    private static LogcatHelper INSTANCE = null;
    private static String PATH_LOGCAT = null;
    private static final String appFolder = "iHealth";
    private static final String filterLogName = "";
    private LogDumper mLogDumper = null;
    private int mPId;

    public void init(Context context) {
        if (Environment.getExternalStorageState().equals("mounted")) {
            PATH_LOGCAT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + appFolder;
        } else {
            PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + appFolder;
        }
        File file = new File(PATH_LOGCAT);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static LogcatHelper getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new LogcatHelper(context);
        }
        return INSTANCE;
    }

    private LogcatHelper(Context context) {
        init(context);
        this.mPId = Process.myPid();
    }

    public void start() {
        if (this.mLogDumper == null) {
            this.mLogDumper = new LogDumper(String.valueOf(this.mPId), PATH_LOGCAT);
        }
        this.mLogDumper.start();
    }

    public void stop() {
        if (this.mLogDumper != null) {
            this.mLogDumper.stopLogs();
            this.mLogDumper = null;
        }
    }

    /* access modifiers changed from: private */
    public class LogDumper extends Thread {
        String cmds = null;
        private Process logcatProc;
        private String mPID;
        private BufferedReader mReader = null;
        private boolean mRunning = true;
        private FileOutputStream out = null;

        public LogDumper(String pid, String dir) {
            this.mPID = pid;
            try {
                this.out = new FileOutputStream(new File(dir, "Log-" + LogcatHelper.getFileName() + ".txt"), true);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            this.cmds = "logcat";
        }

        public void stopLogs() {
            this.mRunning = false;
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
//            String line;
//            try {
//                this.logcatProc = Runtime.getRuntime().exec(cmds);
//                this.mReader = new BufferedReader(new InputStreamReader(this.logcatProc.getInputStream()), 1024);
//                while (this.mRunning && (line = this.mReader.readLine()) != null && this.mRunning) {
//                    if (!(line.length() == 0 || this.out == null || !line.contains(this.mPID))) {
//                        this.out.write((line + "\n").getBytes());
//                    }
//                }
////                if (this.logcatProc != null) {
////                    this.logcatProc.destroy();
////                    this.logcatProc = null;
////                }
//                if (this.mReader != null) {
//                    try {
//                        this.mReader.close();
//                        this.mReader = null;
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//                if (this.out != null) {
//                    try {
//                        this.out.close();
//                    } catch (IOException e2) {
//                        e2.printStackTrace();
//                    }
//                    this.out = null;
//                }
//            } catch (IOException e3) {
//                e3.printStackTrace();
//                if (this.logcatProc != null) {
//                    this.logcatProc.destroy();
//                    this.logcatProc = null;
//                }
//                if (this.mReader != null) {
//                    try {
//                        this.mReader.close();
//                        this.mReader = null;
//                    } catch (IOException e4) {
//                        e4.printStackTrace();
//                    }
//                }
//                if (this.out != null) {
//                    try {
//                        this.out.close();
//                    } catch (IOException e5) {
//                        e5.printStackTrace();
//                    }
//                    this.out = null;
//                }
//            } catch (Throwable th) {
//                if (this.logcatProc != null) {
//                    this.logcatProc.destroy();
//                    this.logcatProc = null;
//                }
//                if (this.mReader != null) {
//                    try {
//                        this.mReader.close();
//                        this.mReader = null;
//                    } catch (IOException e6) {
//                        e6.printStackTrace();
//                    }
//                }
//                if (this.out != null) {
//                    try {
//                        this.out.close();
//                    } catch (IOException e7) {
//                        e7.printStackTrace();
//                    }
//                    this.out = null;
//                }
//                throw th;
//            }
        }
    }

    public static String getFileName() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
    }

    public static String getDateEN() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()));
    }
}
